﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iOak.Web.TemplateHelper;
using iOak.Business.Resumes;
using iOak.Business.Campaigns;

namespace iOak.Web.ResumeTemplates
{
    public partial class WageSubsidyTemplate : BaseUserControl
    {
        private static readonly ResumeTemplateService itemResumeTemplateService = new ResumeTemplateService();
        private static readonly ResumeFieldService itemResumeFieldService = new ResumeFieldService();
        private static readonly ResumeTemplateFieldsService itemResumeTemplateFieldsService = new ResumeTemplateFieldsService();
        private static readonly ResumeDetailService itemResumeDetailService = new ResumeDetailService();
        private static readonly ResumeService itemResumeService = new ResumeService();
        private static readonly ApplicationService itemApplicationService = new ApplicationService();
        private static readonly ApplicationCandidateService itemApplicationCandidateService = new ApplicationCandidateService();

        /// <summary>
        /// 预览时初始化
        /// </summary>
        /// <param name="resumrTemplateId"></param>
        /// <param name="resumeId"></param>
        public override void BindData(Guid resumrTemplateId, Guid resumeId)
        {
            ResumeDetailService itemResumeDetailService = new ResumeDetailService();
            // 获取模板对象
            var listTemplate = TemplateCommonViewHelper.GetTemplateFileList(resumrTemplateId);

            // 根据ID获取值
            var valueList = itemResumeDetailService.GetListByResumeId(resumeId);

            // 绑定数据
            TemplateCommonHelper.InitializationTemplate(listTemplate, this, valueList);

            divButtons.Visible = false;
        }

        /// <summary>
        /// 保存时初始化
        /// </summary>
        /// <param name="resumrTemplateId"></param>
        /// <param name="resumeId"></param>
        /// <param name="applicationId"></param>
        /// <param name="applicationCandidatesId"></param>
        public override void BindData(Guid resumrTemplateId, string resumeId, Guid applicationId, string applicationCandidatesId, bool usedByAdmin = false)
        {
            // 获取模板ID

            hidResumeTemplateId.Value = resumrTemplateId.ToString();

            // 获取活动ID
            hidApplicationId.Value = Convert.ToString(applicationId);
            var itemApplication = itemApplicationService.Retrieve(new Guid(hidApplicationId.Value));
            hidCampaignId.Value = itemApplication.CampaignId.ToString();

            // 获取详细ID
            hidApplicationCandidatesId.Value = applicationCandidatesId;
            hidResumeId.Value = resumeId;
            Guid id = Guid.Empty;
                if (Guid.TryParse(resumeId,out id)) {
                    if (id.Equals(Guid.Empty)) {
                        hidResumeId.Value = Guid.NewGuid().ToString();
                    }
                }
                else {
                    hidResumeId.Value = Guid.NewGuid().ToString();
                }


            // 获取模板对象
            var listTemplate = TemplateCommonHelper.GetTemplateFileList(resumrTemplateId);

            // 根据ID获取值
            var valueList = itemResumeDetailService.GetListByResumeId(new Guid(hidResumeId.Value));

            // 绑定数据
            TemplateCommonHelper.InitializationTemplate(listTemplate, this, valueList, false);

            this.ViewState["usedByAdmin"] = usedByAdmin;
            if (usedByAdmin)
            {
                btnSaveAndAdd.Visible = false;
                lblOr.Visible = false;
                lnkCancel.Visible = false;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            // 获取模板对象
            var listTemplate = TemplateCommonHelper.GetTemplateFileList(new Guid(hidResumeTemplateId.Value));
            string strCheck = TemplateCommonHelper.SaveCheck(listTemplate, this);

            if (!string.IsNullOrWhiteSpace(strCheck))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "jAlert", "$(document).ready( function(){jAlert('" + strCheck + "')" + "});", true);
                return;
            }
            string applicationCandidateId = hidApplicationCandidatesId.Value;
            TemplateCommonHelper.TempleSave(this, ref applicationCandidateId, hidApplicationId.Value, hidResumeId.Value, hidCampaignId.Value,
                hidResumeTemplateId.Value);

            hidApplicationCandidatesId.Value = applicationCandidateId;

            // 跳转
            bool usedByAdmin = bool.Parse(this.ViewState["usedByAdmin"].ToString());
            if (!usedByAdmin)
            {
                Response.Redirect("CandidateList.aspx?campaignId=" + hidCampaignId.Value);
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, typeof(Page), "bindTemplate", "bindTemplate();", true);
            }
        }

        protected void btnSaveAndAdd_Click(object sender, EventArgs e)
        {
            // 获取模板对象
            var listTemplate = TemplateCommonHelper.GetTemplateFileList(new Guid(hidResumeTemplateId.Value));
            string strCheck = TemplateCommonHelper.SaveCheck(listTemplate, this);

            if (!string.IsNullOrWhiteSpace(strCheck))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "jAlert", "jAlert(" + strCheck + ")", true);

                return;
            }

            // 先保存
            string applicationCandidateId = hidApplicationCandidatesId.Value;
            TemplateCommonHelper.TempleSave(this, ref applicationCandidateId, hidApplicationId.Value, hidResumeId.Value, hidCampaignId.Value,
                hidResumeTemplateId.Value);

            TemplateCommonHelper.ClearTemplate(listTemplate, this);

            // 从新设定Resume值
            hidResumeId.Value = Guid.NewGuid().ToString();
            hidApplicationCandidatesId.Value = string.Empty;
        }

        protected void lnkCancel_Click(object sender, EventArgs e)
        {
            // 获取模板对象
            var listTemplate = TemplateCommonHelper.GetTemplateFileList(new Guid(hidResumeTemplateId.Value));

            TemplateCommonHelper.ClearTemplate(listTemplate, this);

            // 跳转
            Response.Redirect("CandidateList.aspx?campaignId=" + hidCampaignId.Value);
        }
    }
}